package com.leyou.mq;
import com.leyou.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.constants.MQConstants.*;
/**
 * @author jshstart
 * @creat 2020-12-16 15:21
 */
//配置rabbit监听器
@Component
public class ItemListener {
    @Autowired
    private SearchService searchService;
    //bindings用来创建队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QueueConstants.SEARCH_ITEM_UP, durable = "true"),
            exchange = @Exchange(name = ExchangeConstants.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = RoutingKeyConstants.ITEM_UP_KEY
    ))
    public void listenItemUp(Long SpuId){
        if (SpuId!=null){
            searchService.saveGoodBySpuId(SpuId);
        }
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QueueConstants.SEARCH_ITEM_DOWN,durable = "true"),
            exchange = @Exchange(name = ExchangeConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key =  RoutingKeyConstants.ITEM_DOWN_KEY
    ))
    public void listenItemDown(Long SpuId){
        if (SpuId!=null){
            searchService.deleteGoodBySpuId(SpuId);
        }
    }
}
